/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.luxoft.corporate.blog.ui;

import com.luxoft.corporate.blog.db.Controller;
import com.luxoft.corporate.blog.db.entities.Topic;
import java.awt.Dimension;
import javax.annotation.PostConstruct;
import javax.swing.Action;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.DocumentFilter;
import javax.swing.text.PlainDocument;

/**
 *
 * @author Egor
 */
public class AddTopicPanel extends JPanel {

    private JTextField topicNameField;
    private JTextArea descriptionField;

    private JButton saveButton;
    private JButton cancelButton;

    public AddTopicPanel() {
        descriptionField = new JTextArea(25, 50);
        topicNameField = new JTextField();
        saveButton = new JButton();
        cancelButton = new JButton();
        init();
    }

    private void init() {
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        ((PlainDocument)topicNameField.getDocument()).setDocumentFilter(new DocumentFilter(){

            @Override
            public void insertString(DocumentFilter.FilterBypass fb, int offset, String string, AttributeSet attr) throws BadLocationException {
                if(string.length() > 19) return;
                 super.insertString(fb, offset, string, attr); //To change body of generated methods, choose Tools | Templates.
            }
            
        });
        
        topicNameField.setPreferredSize(new Dimension(300, 20));
        saveButton.setPreferredSize(new Dimension(40, 20));
        cancelButton.setPreferredSize(new Dimension(40, 20));
        descriptionField.setPreferredSize(new Dimension(300, 40));

        add(Box.createVerticalStrut(5));
        add(topicNameField);
        add(Box.createVerticalStrut(5));
        JScrollPane descriptionScrollPane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        descriptionScrollPane.getViewport().add(descriptionField);
        add(descriptionScrollPane);
        add(Box.createVerticalStrut(5));

        Box buttonsBox = Box.createHorizontalBox();
        buttonsBox.add(Box.createHorizontalGlue());
        buttonsBox.add(saveButton);
        add(Box.createHorizontalStrut(5));
        buttonsBox.add(cancelButton);
        buttonsBox.add(Box.createHorizontalGlue());
        add(Box.createVerticalStrut(5));

        add(buttonsBox);

    }

    public void setSaveAction(Action action) {
        saveButton.setAction(action);
    }

    public void setCancelAction(Action action) {
        cancelButton.setAction(action);
    }
    
    
    public void clear(){
        topicNameField.setText("");
        descriptionField.setText("");
    }
    
    
    public Topic getTopic(){
        Topic topic = new Topic();
        topic.setTopicDesctription(descriptionField.getText());
        topic.setTopicName(topicNameField.getText());
        return topic;
    }
}
